/**
 * Universidade Federal do Rio Grande do Norte 
 * Departamento de Engenharia de Computacao e Automacao
 *
 * @date 03/04/2013
 * @author felipe
 */
package br.ufrn.labmetrol.excel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * Classe para tratar a escrita dos arquivos em xls.
 *
 */
public class ExcelManager {

    private Workbook wb;
    private Sheet sheet;

    public ExcelManager() throws FileNotFoundException {
        wb = new HSSFWorkbook();
        sheet = wb.createSheet("Dados PRO3600");
    }

    public void writeExcelFile(String[][] data, String path) throws FileNotFoundException, IOException {
        for (int i = 0; i < data.length; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < data[1].length; j++) {
                Cell cell = row.createCell(j);
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(data[i][j].replace(".", ","));
            }
        }
        File f;
        if (path.contains(".xls")) {
            f = new File(path);
        } else {
            f = new File(path + ".xls");
        }
        FileOutputStream fileOut = new FileOutputStream(f);
        wb.write(fileOut);
        fileOut.close();
    }

    public Workbook getWb() {
        return wb;
    }

    public void setWb(Workbook wb) {
        this.wb = wb;
    }
}
